﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using AMS.Web.BaseData;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.Helper;

namespace AMS.Web
{
    public partial class MLTest : VitCode.BaseDataPageCreator.Setting.AMSPage
    {
        StringBuilder content = new StringBuilder();
        TextWriter tw_old, tw_new;
        FieldInfo tw_field;

        protected void Page_Load(object sender, EventArgs e)
        {
            //var context = HttpContext.Current;

            //tw_old = context.Response.Output;//Response原来的OutPut
            //tw_new = new StringWriter(content);//一个StringWriter,用来获取页面内容
            //var type_rp = context.Response.GetType();
            ////通过反射获取对象的私有字段
            //tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            //tw_field.SetValue(context.Response, tw_new);
        }

        //protected override void Render(HtmlTextWriter writer)
        //{
        //    base.Render(writer);
        //    //替换回Response的OutPut
        //    tw_field.SetValue(HttpContext.Current.Response, tw_old);
        //    string strInfo = content.ToString();
        //    new LanguageHelper().SetLanguageInfoForString(ref strInfo);
        //    HttpContext.Current.Response.Write(strInfo);
        //}


        protected void Button1_Click(object sender, EventArgs e)
        {
            Alert.MessageBox("条件选择器");
        }

        //protected override void Render(HtmlTextWriter writer)
        //{
        //    StringWriter sw = new StringWriter();
        //    base.Render(new HtmlTextWriter(sw));
        //    new LanguageHelper().SetLanguageInfo(writer, sw);
        //}



        
        protected void Button2_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            if(di==null)
                return;
            if(di!=null && di.Parent!=null)
                di = di.Parent;
            string dirPath = di.FullName;
            string strLanguageXmlPath = AppDomain.CurrentDomain.BaseDirectory + "Language\\CN.xml";
            if (File.Exists(strLanguageXmlPath))
                File.Delete(strLanguageXmlPath);
            XmlDocument doc = new XmlDocument();
            FileStream fs = new FileStream(strLanguageXmlPath, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            XmlElement settingNode = doc.CreateElement("Setting");
            doc.AppendChild(settingNode);
            try
            {
                doc.Save(sw);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
            StringCollection scFileNames = new StringCollection();
            LanguageHelper.GetDirAllFileNames(dirPath, new string[] {"*.as?x", "*.cs","*.xml"}, scFileNames);
            LanguageHelper.SetLanguageInfoFile(scFileNames, strLanguageXmlPath);
            Alert.MessageBox("OK");
        }
    }
}
